package 
{
	import away3d.events.MouseEvent3D;
	import fl.controls.CheckBox;
	import fl.controls.ComboBox;
	import fl.events.ComponentEvent;
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.display.BitmapData;
	
	/**
	 * ...
	 * @author ...
	 */
	public class ColorPicker extends MovieClip {
		
		public var bg:MovieClip;
		public var dropDown:MovieClip;
		public var colorName:TextField;
		public var selectedSwatch:MovieClip;
		public var selectedIndex:int;
		public var roofTextures:Array;
		
		public function ColorPicker() {
			hideDropDown();
		}
		
		public function init(textures:Array) {
			roofTextures = textures;
			var xPos:int = 8;
			var yPos:int = 28;
			for (var i:int = 0; i < textures.length; i++) {
				var swatch:Sprite = textures[i].getSwatch();
				swatch.name = textures[i].name;
				swatch.addEventListener(MouseEvent.MOUSE_OVER, rollOverSwatch);
				swatch.addEventListener(MouseEvent.MOUSE_OUT, rollOutSwatch);
				swatch.addEventListener(MouseEvent.CLICK, selectSwatch);
				swatch.x = xPos;
				swatch.y = yPos;
				dropDown.addChild(swatch);
				xPos += swatch.width + 8;
				if ((i+1) % 5 == 0) {
					yPos += swatch.height + 8;
					xPos = 8;
				}
			}
			dropDown.bg.height = yPos;
			selectRandom();
		}
		
		private function selectRandom():void {
			var randomIndex = int(Math.random() * roofTextures.length);
			selectedIndex = randomIndex;
			resetTopSwatch();
		}
		
		public function toggleEnabledCheckbox(e:ComponentEvent):void {
			if ((e.target as CheckBox).selected) disable();
			else enable();
		}
		
		public function toggleEnabledDropdown(e:Event):void {
			if ((e.target as ComboBox).selectedIndex == 1) disable();
			else enable();
		}
		
		public function disable():void {
			alpha = .5;
			if (dropDown.visible) hideDropDown();
			try { removeEventListener(MouseEvent.MOUSE_OUT, rollOut); } catch (e:Error) { }
			try { removeEventListener(MouseEvent.MOUSE_OVER, rollOver); } catch (e:Error) { }
			try { removeEventListener(MouseEvent.CLICK, showDropDown); } catch (e:Error) { }
		}
		
		public function enable():void {
			alpha = 1;
			addEventListener(MouseEvent.MOUSE_OVER, rollOver);
			addEventListener(MouseEvent.MOUSE_OUT, rollOut);
			addEventListener(MouseEvent.CLICK, showDropDown);
		}
		
		private function showDropDown(E:MouseEvent):void {
			bg.gotoAndStop(2);
			dropDown.visible = true;
			removeEventListener(MouseEvent.MOUSE_OVER, rollOver);
			removeEventListener(MouseEvent.MOUSE_OUT, rollOut);
			removeEventListener(MouseEvent.CLICK, showDropDown);
			
			addEventListener(MouseEvent.CLICK, hideDropDown);
		}
		
		private function hideDropDown(e:MouseEvent = null):void {
			bg.gotoAndStop(1);
			dropDown.visible = false;
			addEventListener(MouseEvent.MOUSE_OVER, rollOver);
			addEventListener(MouseEvent.MOUSE_OUT, rollOut);
			addEventListener(MouseEvent.CLICK, showDropDown);
			removeEventListener(MouseEvent.CLICK, hideDropDown);
		}
		
		private function rollOver(e:MouseEvent):void {
			bg.gotoAndStop(2);
		}
		
		private function rollOut(e:MouseEvent):void {
			bg.gotoAndStop(1);
		}
		
		private function rollOverSwatch(e:MouseEvent):void {
			var dropShadow:DropShadowFilter = new DropShadowFilter(2, 90, 0, .75, 3, 3);
			e.target.filters = [dropShadow];
			colorName.text = e.target.name;
			
			var bmd:BitmapData = new BitmapData(e.target.width, e.target.height);
			var tmpFilters:Array = e.target.filters;
			e.target.filters = [];
			bmd.draw(e.target as Sprite);
			e.target.filters = tmpFilters;
			var duplicateSwatch:Sprite = new Sprite();
			duplicateSwatch.addChild(new Bitmap(bmd));
			setSelectedSwatch(duplicateSwatch);
		}
		
		private function rollOutSwatch(e:MouseEvent):void {
			e.target.filters = [];
			resetTopSwatch();
		}
		
		private function selectSwatch(e:MouseEvent):void {
			selectedIndex = dropDown.getChildIndex(e.target as Sprite) - 1;
		}
		
		private function resetTopSwatch():void {
			var roofTexture:BarnTexture = roofTextures[selectedIndex]; 
			colorName.text = roofTexture.name;
			var swatch:Sprite = roofTexture.getSwatch();
			swatch.height -= 1;
			swatch.y = 1;
			setSelectedSwatch(swatch);
		}
		
		private function setSelectedSwatch(swatch:Sprite):void {
			swatch.height = 13;
			for (var i = 0; selectedSwatch.numChildren; i++) selectedSwatch.removeChildAt(0);
			selectedSwatch.addChild(swatch);
		}
		
	}

}